package com.zhongying.jf.util.vaild;

import com.zhongying.jf.ResConstants;
import com.zhongying.jf.exception.ZhongyingiException;
import com.zhongying.jf.rule.Rule;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 数据验证工具类 针对各种验证提供三种传参方式 1:message 2:code,message 3:rule(验证规则基类)
 * <hr>
 * 若参数不满足条件,则抛出异常:new ZhongyingiException(message)<br>
 * <hr>
 * 参考 spring-core/src/main/java/org/springframework/util/Assert.java
 * @author zengli
 */
public final class ValidatorAssert {

    private ValidatorAssert() {
    }

    public static void isTrue(boolean expression, String message)
            throws ZhongyingiException {
        isTrue(expression, ResConstants.FAIL.getCode(), message);
    }

    public static void isTrue(boolean expression, String code, String message)
            throws ZhongyingiException {
        if (!expression) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void isTrue(boolean expression, Rule rule)
            throws ZhongyingiException {
        if (!expression) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

    public static void isNull(Object object, String message)
            throws ZhongyingiException {
        isNull(object, ResConstants.FAIL.getCode(), message);
    }

    public static void isNull(Object object, String code, String message)
            throws ZhongyingiException {
        if (object != null) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void isNull(Object object, Rule rule)
            throws ZhongyingiException {
        if (object != null) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

    public static void notNull(Object object, String message)
            throws ZhongyingiException {
        notNull(object, ResConstants.FAIL.getCode(), message);
    }

    public static void notNull(Object object, String code, String message)
            throws ZhongyingiException {
        if (object == null) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void notNull(Object object, Rule rule)
            throws ZhongyingiException {
        if (object == null) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

    public static void notNull(Object... objects)
            throws ZhongyingiException {
        notNull(objects,"入参不能为空!");
        for (int i = 0; i < objects.length ; i++) {
            Object o = objects[i];
            notNull(o,"入参不能为空!");
            if (o instanceof List) {
                isTrue(!((List) o).isEmpty(),"入参不能为空!");
            } else if (o instanceof Map) {
                isTrue(!((Map) o).isEmpty(),"入参不能为空!");
            } else if (o.getClass() == String.class) {
                notEmpty(o.toString(),"入参不能为空!");
            }
        }
    }

    public static void isEmpty(String text, String message) throws ZhongyingiException {
        isEmpty(text, ResConstants.FAIL.getCode(), message);
    }

    public static void isEmpty(String text, String code, String message) throws ZhongyingiException {
        if (!StringUtils.isEmpty(text)) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void isEmpty(String text, Rule rule) throws ZhongyingiException {
        if (!StringUtils.isEmpty(text)) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

    public static void notEmpty(String text, String message)
            throws ZhongyingiException {
        notEmpty(text, ResConstants.FAIL.getCode(), message);
    }

    public static void notEmpty(String text, String code, String message)
            throws ZhongyingiException {
        if (StringUtils.isEmpty(text)) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void notEmpty(String text, Rule rule)
            throws ZhongyingiException {
        if (StringUtils.isEmpty(text)) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

    public static void isBlank(String text, String message) throws ZhongyingiException {
        isBlank(text, ResConstants.FAIL.getCode(), message);
    }

    public static void isBlank(String text, String code, String message) throws ZhongyingiException {
        if (!StringUtils.isBlank(text)) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void isBlank(String text, Rule rule) throws ZhongyingiException {
        if (!StringUtils.isBlank(text)) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

    public static void notBlank(String text, String message)
            throws ZhongyingiException {
        notBlank(text, ResConstants.FAIL.getCode(), message);
    }

    public static void notBlank(String text, String code, String message)
            throws ZhongyingiException {
        if (StringUtils.isBlank(text)) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void notBlank(String text, Rule rule)
            throws ZhongyingiException {
        if (StringUtils.isBlank(text)) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

    public static void isNumeric(String text, String message)
            throws ZhongyingiException {
        isNumeric(text, ResConstants.FAIL.getCode(), message);
    }

    public static void isNumeric(String text, String code, String message)
            throws ZhongyingiException {
        if (!StringUtils.isNumeric(text)) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void isNumeric(String text, Rule rule)
            throws ZhongyingiException {
        if (!StringUtils.isNumeric(text)) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

    public static void isNumericOrBlank(String text, String message)
            throws ZhongyingiException {
        isNumeric(text, ResConstants.FAIL.getCode(), message);
    }

    public static void isNumericOrBlank(String text, String code, String message)
            throws ZhongyingiException {
        if (StringUtils.isNotBlank(text)) {
            if (!StringUtils.isNumeric(text)) {
                throw new ZhongyingiException(code, message);
            }
        }
    }

    public static void isNumericOrBlank(String text, Rule rule)
            throws ZhongyingiException {
        if (StringUtils.isNotBlank(text)) {
            if (!StringUtils.isNumeric(text)) {
                throw new ZhongyingiException(rule.code(), rule.content());
            }
        }
    }

    public static void notEmpty(Collection<?> collection, String message)
            throws ZhongyingiException {
        notEmpty(collection, ResConstants.FAIL.getCode(), message);
    }

    public static void notEmpty(Collection<?> collection, String code, String message)
            throws ZhongyingiException {
        if (collection == null || collection.isEmpty()) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void notEmpty(Collection<?> collection, Rule rule)
            throws ZhongyingiException {
        if (collection == null || collection.isEmpty()) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

    public static void isEmpty(Collection<?> collection, String message)
            throws ZhongyingiException {
        isEmpty(collection, ResConstants.FAIL.getCode(), message);
    }

    public static void isEmpty(Collection<?> collection, String code, String message)
            throws ZhongyingiException {
        if (null != collection && !collection.isEmpty()) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void isEmpty(Collection<?> collection, Rule rule)
            throws ZhongyingiException {
        if (null != collection && !collection.isEmpty()) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

    public static void notEmpty(Object[] array, String message)
            throws ZhongyingiException {
        notEmpty(array, ResConstants.FAIL.getCode(), message);
    }

    public static void notEmpty(Object[] array, String code, String message)
            throws ZhongyingiException {
        if (array == null || array.length == 0) {
            throw new ZhongyingiException(code, message);
        }
    }

    public static void notEmpty(Object[] array, Rule rule)
            throws ZhongyingiException {
        if (array == null || array.length == 0) {
            throw new ZhongyingiException(rule.code(), rule.content());
        }
    }

}